package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.EmailConfig;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.vo.EmailVo;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
@Slf4j
public class  EmailUtils  {

    private static final String commonModelHead = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><title>Title</title></head><style>    table {        font-size: 14px;        border-collapse: collapse;        width: 70%;        text-align: left;        line-height: 25px;        margin:0 auto;        margin-left: 0%;    }    table tr {        border: solid 1px #cccccc;        border-left: none;        border-right: none;        border-top: none;    }    table td{        height: 50px;    }    .left-td{        color: #FF9300;        font-size: 18px;        font-weight: bold;    }    .left-td div{        width: 100%;        height: 100%;        margin-top: 15px;    }    .right-td{color: #000000;font-size: 16px;font-weight: 500;}    h1{font-weight: bold;}    .div-title{font-weight: bold;font-size: 22px;}</style><body><table >";
    private static final String commonModelFoot = "</table><br><br><br></body></html>";

    private static JSONObject buildParam(EmailVo emailVo) throws Exception{

        JSONObject body = new JSONObject();

        body.put("subject",emailVo.getSubject());
        body.put("cc",emailVo.getCc());
        body.put("addressees",emailVo.getAddressees());
        long timestamp = System.currentTimeMillis();
        String sign = "appId=" + EmailConfig.getAppId() + "&apiKey=" + EmailConfig.getApiKey() + "&timestamp=" + timestamp;

        body.put("sign",SignUtils.encryptBASE64(sign.getBytes()));
        body.put("timestamp",timestamp);

        return body;
    }

    public static void sendCommonEmail(EmailVo emailVo){
        try{
            JSONObject body = buildParam(emailVo);

            String commonModelTitle = String.format("<div class=\"div-title\">%s</div><br>",emailVo.getTitle());
            StringBuffer commonModelContent = new StringBuffer();
            for(String key:emailVo.getParam().keySet()){//keySet获取map集合key的集合  然后在遍历key即可
                commonModelContent.append(String.format("<tr><td class=\"left-td\" width=200 >%s</td><td class=\"right-td\" width=400>%s</td></tr>",key,emailVo.getParam().get(key)));
            }
            String content = commonModelHead+commonModelTitle+commonModelContent+commonModelFoot;

            body.put("content",content);

            Map<String,String> map = new HashMap<>();
            map.put("Content-Type","application/json");
            String res = HttpUtils.sendPost(EmailConfig.getSendEmailUrl(), body.toString(), map);
            log.info("邮件响应:{}",res);
        }catch (Exception e){
            log.error("邮件发送",e);
        }
    }

    // 用户反馈邮箱参数的拼接
    public static String sendfeedbackMailParameterAssemblyEmail(EmailVo emailVo) throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("subject",emailVo.getSubject());
        jsonObject.put("addressees",emailVo.getAddressees());
        jsonObject.put("cc",emailVo.getCc());
        long timestamp = System.currentTimeMillis();
        String sign = "appId=" + EmailConfig.getAppId() + "&apiKey=" + EmailConfig.getApiKey();

        jsonObject.put("sign",SignUtils.encryptBASE64(sign.getBytes()));
        jsonObject.put("timestamp",timestamp);
        jsonObject.put("content",emailVo.getFeedBackEmailParam());

        Map<String,String> map = new HashMap<>();
        map.put("Content-Type","application/json");
        String result = HttpUtils.sendPost(EmailConfig.getSendEmailUrl(), jsonObject.toString(), map);
        return result;
    }

    public static void sendCustomizeEmail(EmailVo emailVo){

        try{
            JSONObject body = buildParam(emailVo);
            body.put("content",emailVo.getContent());
            Map<String,String> map = new HashMap<>();
            map.put("Content-Type","application/json");
            String res = HttpUtils.sendPost(EmailConfig.getSendEmailUrl(), body.toString(), map);
            log.info("邮件响应:{}",res);
        }catch (Exception e){
            log.error("邮件发送",e);
        }
    }

}

